package com.bw.mallshop;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.bw.mallshop.databinding.ActivityMainBinding;
import com.bw.mallshop.mvvm.viewmodel.BottomViewModel;
import com.bw.mvvm_common.mvvm.view.BaseActivity;
import com.bw.mvvm_common.weidget.BottomButton;

public class MainActivity extends BaseActivity<ActivityMainBinding, BottomViewModel> implements BottomButton.BottomSelectListenner {

    private LinearLayout frameLiner;
    private BottomButton Home;
    private BottomButton Category;
    private BottomButton ShopCar;
    private BottomButton Mine;
    private FrameLayout mainFgGroup;
    private Fragment goodsFragment, categoryFragment,shopfragment,msgfragment,userfragment;
    private BottomButton xx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EventBus.getDefault().register(this);
        initView();
        Home.setContact("首页")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.hhh)
                .setSelImg(R.drawable.no_hhh)
                .setNamesize(6)
                .setSelsize(8)
                .setListenner(this)
                .refresh();
        Category.setContact("分类")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.btn_nav_category_normal)
                .setSelImg(R.drawable.btn_nav_category_press)
                .setNamesize(6)
                .setSelsize(8)
                .setListenner(this)
                .refresh();
        ShopCar.setContact("购物车")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.btn_nav_cart_normal)
                .setSelImg(R.drawable.btn_nav_cart_press)
                .setNamesize(6)
                .setSelsize(8)
                .setListenner(this)
                .refresh();
        xx.setContact("消息")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.btn_nav_msg_normal)
                .setSelImg(R.drawable.btn_nav_msg_press)
                .setNamesize(6)
                .setSelsize(8)
                .setListenner(this)
                .refresh();
        Mine.setContact("我的")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.btn_nav_user_normal)
                .setSelImg(R.drawable.btn_nav_user_press)
                .setNamesize(6)
                .setSelsize(8)
                .setListenner(this)
                .refresh();
        onSelect(R.id.Home);

    }

    @Override
    public int bindLayout() {
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this, true);
//        Fragment goodsFragment = (Fragment) ARouter.getInstance()
//                .build("/com/bw/goods/mvvm/view/fragment/HomeGoodsFragment").navigation();
//        getSupportFragmentManager().beginTransaction().add(R.id.main_fg_group,goodsFragment).commit();
        return R.layout.activity_main;

    }
//    @Subscribe(sticky = true)
//    public void text(String s){
//        if (s.equals("a")){
//
////                fragmentTransaction.hide(goodsFragment).commit();
//
//

//
//        }
//
//    }

    @Override
    public int initVerable() {
        return BR.t;
    }


    @Override
    public void onSelect(int id) {

        clearBottomsel();
        hideFagment();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        switch (id) {
            case R.id.Home:
                Home.setSetFlag(true);
                if (goodsFragment == null) {
                    goodsFragment = (Fragment) ARouter.getInstance()
                            .build("/com/bw/goods/mvvm/view/fragment/HomeGoodsFragment").navigation();
                    ft.add(R.id.main_fg_group, goodsFragment);
                } else {
                    ft.show(goodsFragment);
                }
                break;
            case R.id.Category:
                Category.setSetFlag(true);
                if (categoryFragment == null) {
                    categoryFragment = (Fragment) ARouter.getInstance()
                            .build("/com/bw/goods/mvvm/view/fragment/CateGoryFragment").navigation();
                    ft.add(R.id.main_fg_group, categoryFragment);
                } else {
                    ft.show(categoryFragment);
                }
                break;
            case R.id.ShopCar:
                ShopCar.setSetFlag(true);
                if (shopfragment == null) {
                    shopfragment = (Fragment) ARouter.getInstance()
                            .build("/shopping/mvvm/view/fragment/ShopFragment").navigation();
                    ft.add(R.id.main_fg_group, shopfragment);
                } else {
                    ft.show(shopfragment);
                }

                break;
            case R.id.Mine:
                Mine.setSetFlag(true);
                if (userfragment==null){
                    userfragment = (Fragment) ARouter.getInstance()
                            .build("/fragment/UserFragment").navigation();
                    ft.add(R.id.main_fg_group, userfragment);
                }else {
                    ft.show(userfragment);
                }
                break;
            case R.id.xx:
                xx.setSetFlag(true);
                if (msgfragment==null){
                    msgfragment = (Fragment) ARouter.getInstance()
                            .build("/view/fragment/MsgFragment").navigation();

                    ft.add(R.id.main_fg_group, msgfragment);

                }else {
                    ft.show(msgfragment);
                }
                break;

//            default:
//                ft.commit();
        }
        ft.commit();

    }

    private void clearBottomsel() {
        Home.setSetFlag(false);
        Category.setSetFlag(false);
        ShopCar.setSetFlag(false);
        Mine.setSetFlag(false);
        xx.setSetFlag(false);


    }

    private void hideFagment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        if (goodsFragment != null)
            ft.hide(goodsFragment);
        if (categoryFragment != null)
            ft.hide(categoryFragment);
        if (msgfragment!=null)
            ft.hide(msgfragment);
        if (userfragment!=null)
            ft.hide(userfragment);
        if (shopfragment!=null)
            ft.hide(shopfragment);
        ft.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }

    private void initView() {

        frameLiner = (LinearLayout) findViewById(R.id.frame_liner);
        Home = (BottomButton) findViewById(R.id.Home);
        Category = (BottomButton) findViewById(R.id.Category);
        ShopCar = (BottomButton) findViewById(R.id.ShopCar);
        Mine = (BottomButton) findViewById(R.id.Mine);
        xx = (BottomButton) findViewById(R.id.xx);
        mainFgGroup = (FrameLayout) findViewById(R.id.main_fg_group);

    }
}